package com.yc.spring.jdbc.biz;

import com.yc.spring.jdbc.dao.AccountDao;
import com.yc.spring.jdbc.dao.OprecordDao;
import com.yc.spring.jdbc.entity.Account;
import com.yc.spring.jdbc.entity.Oprecored;
import com.yc.spring.jdbc.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class BankBiz {
    @Autowired
    AccountDao adao;
    @Autowired
    OprecordDao oDao;
    @Autowired
    JdbcTemplate jdbcTemplate;
    //存款
    //rollbackFor->出现异常回滚，针对编译器异常，默认会对运行时异常时进行回滚，不会对编译器异常回滚
    @Transactional(rollbackFor = BizException.class,
    noRollbackFor=ArithmeticException.class)//默认回滚，设置为不回滚
    public void deposit(long id,double money) throws BizException {
        Oprecored oprecored=new Oprecored();
        oprecored.setAid(id);
        oprecored.setOpmoney(money);
        oDao.insert(oprecored);
//        if(money>5000){
//            //编译器异常
//            throw  new BizException("存款大于5000");
//        }
//        int i=1/0;
        adao.updateBalanceById(id,money);
    }
    //取款
    public void withdraw(long id,double money){

    }
    //转账
    public void transfer(long id1,long id2,double money){

    }
    //登录
    public Result login(Account account){
        if(account.getPassword()==null||account.getPassword().equals("")){
            return new Result(501,"密码不能为空",null);
        }
        if (account.getId()==null){
            return new Result(502,"账户不能为空",null);
        }
        account=adao.login(account);
        if (account==null){

            return new Result(200,"登录成功",null);
        }
        return new Result(503,"系统异常",null);
    }
}
